package piblanes.graphics;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;

import piblanes.ControllerSignal;
import piblanes.Game;
import piblanes.Player;
import piblanes.apache.Polygon2D;
import piblanes.KeyboardController;

/**
 *
 * @author senator
 */
public class SimpleVisualisator extends JFrame {

    public SimpleVisualisator(Game game_) {
        game = game_;

        initFrame();


        Timer gameTimer = new Timer(20, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                game.tick();
                canva.repaint();
            }
        });

        gameTimer.start();
    }

    private void initFrame() {
        setSize(300, 300);

        setDefaultCloseOperation(EXIT_ON_CLOSE);

        canva = new Canva();

        add(canva);

        pack();
        setVisible(true);
    }

    private Canva canva;
    private Game game;
    private ArrayList<KeyboardController> controllers;
    private static final long serialVersionUID = 1;

    private class Canva extends JPanel {
        public Canva() {
            InitComponent();
            pack();
        }

        private void InitComponent() {
            setOpaque(true);
            setFocusable(true);
            setPreferredSize(new Dimension(300,300));

            ArrayList<Player> players = game.getPlayers();
            controllers = new ArrayList <KeyboardController> (players.size());

            Map <Integer, ControllerSignal> simpleMap = new HashMap<Integer, ControllerSignal>();
            simpleMap.put(KeyEvent.VK_RIGHT, ControllerSignal.RIGHT);
            simpleMap.put(KeyEvent.VK_LEFT, ControllerSignal.LEFT);

            for (int i = 0; i<players.size(); ++i) {
                controllers.add(new KeyboardController(players.get(i), simpleMap));
                addKeyListener(controllers.get(i).getKeyHandler());
            }
        }

        @Override
        public void paintComponent(Graphics g) {
            Graphics2D g2 = (Graphics2D) g;
            g2.clearRect(0, 0, 300, 300);

            AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
            tx.translate(0, -300);
            g2.setTransform(tx);

            g2.scale(3,3);

            ArrayList<Player> players = game.getPlayers();
            for (Player currentPlayer : players) {
                Polygon2D playerShape = currentPlayer.getGeometricalState().getMovedRotatedShape();
                for (int i = 0; i<playerShape.npoints-1; ++i)
                    g2.draw(new Line2D.Double(playerShape.xpoints[i], playerShape.ypoints[i], playerShape.xpoints[i+1],
                            playerShape.ypoints[i+1]));

                g2.draw(new Line2D.Double(playerShape.xpoints[0], playerShape.ypoints[0],
                        playerShape.xpoints[playerShape.npoints-1],playerShape.ypoints[playerShape.npoints-1]));

            }
        }
    }
}


